package com.hwadee.hwadeedemo01.controller;


import com.hwadee.hwadeedemo01.pojo.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: RavenLyn
 * @Date: 2024-05-13
 */
@RestController
@RequestMapping("/api/user") //@RestController 这个注解一般添加在controller上，可以让所有请求类型的根路径
public class UserController {

    @GetMapping //只能添加在方法上，表示接收get请求（查找请求）
    public ResponseEntity<String> getUsers() {
        // 使用name和age参数做一些处理
        String response = "Name: " + "张三" + ", Age: " + 18;
        return ResponseEntity.ok(response);
    }

    @GetMapping
    public ResponseEntity<String> getUserByNameAndAge(
            @RequestParam(name = "name", required = false, defaultValue = "Unknown") String name,
            @RequestParam(name = "age", required = false, defaultValue = "0") int age) {
        /*
        用于解析URL查询字符串参数，例子中你可以通过http://localhost:8080/api/user?name=John&age=30
        来访问这个端点，并传递name和age参数
        */
        String response = "Name: " + name + ", Age: " + age;
        return ResponseEntity.ok(response);
    }

    @GetMapping("/getUserById/{id}")
    public ResponseEntity<String> getUserById(@PathVariable Long id) {
        // @PathVariable 用于接收URL路径参数，http://localhost:8080/api/user/getUserById/123
        String response = "User with ID: " + id;
        return ResponseEntity.ok(response);
    }

    @PostMapping("/save")//用于接收保存数据请求
    public ResponseEntity<String> save(@RequestBody User user){
        //对数据进行保存
        return ResponseEntity.ok("user");
    }

    @PutMapping("/updateById")//用于接收更新请求
    public ResponseEntity<String> updateById(@RequestBody User user) {
        //userService.updateById(user)
        return ResponseEntity.ok("user");
    }

    @DeleteMapping("/remove/{id}")
    public ResponseEntity<String> removeById(@PathVariable Long id) {
        //UserService.removeById(id)
        return ResponseEntity.ok("user");
    }
}
